package com.jyw.business.obstacles.domain.dto.web;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.collect.Lists;
import com.jyw.business.obstacles.constant.OrderCurrStateConstant;
import com.jyw.business.obstacles.constant.OrderDealTypeConstant;
import com.jyw.business.obstacles.domain.ObsOrderFault;
import com.jyw.business.obstacles.domain.ObsOrderInfo;
import com.jyw.business.obstacles.domain.ObsOrderSend;
import com.jyw.common.annotation.Excel;
import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.SecurityUtils;
import com.jyw.system.domain.vo.SimpleFileVo;
import com.jyw.system.utils.DeptUtils;
import com.jyw.system.utils.FileUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 沈峻
 * @ClassName AddOrderDto
 * @Description 创建工单请求实体
 * @Date 2023/8/10 15:34
 **/
@ApiModel("创建工单请求实体")
@Data
public class BaseOrderAddDto {

    /** 创建时间 */
    @ApiModelProperty("创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date createTime;

    @ApiModelProperty("工单来源")
    private Integer source;

    /** 实际报修地址 */
    @ApiModelProperty("实际报修地址")
    private Long actualRepairPlace;

    @ApiModelProperty("派单对象类型")
    private Integer sendObjType;

    @ApiModelProperty("派单所属小组")
    private Long sendTeam;

    /** 指派类型:1由我处理，2派单 */
    @ApiModelProperty("指派类型:1由我处理，2派单")
    private Integer sendType;

    /** 故障分类 */
    @ApiModelProperty("故障分类")
    private String faultType;

    @ApiModelProperty("故障描述")
    private String faultRemark;

    @ApiModelProperty("知识库ID")
    private Long learningId;

    @ApiModelProperty("预计工时")
    private Integer reqTime;

    @ApiModelProperty("期望完成时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date completeTime;

    @ApiModelProperty("紧急程度")
    private Integer degree;

    @ApiModelProperty("影响范围")
    private Integer rangeType;

    @ApiModelProperty("响应类型")
    private Integer responseType;

    @ApiModelProperty("预约时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date appointment;

    /** 资产标识(系统资产) */
    @ApiModelProperty("资产标识(系统资产)")
    private Long assetsId;

    @ApiModelProperty("资产名称")
    private String equipment;

    /** 故障设备位置 */
    @ApiModelProperty("资产位置")
    private String equipmentAddress;

    /** 故障设备位置id */
    @ApiModelProperty("资产位置id")
    private Long equipmentAddressId;

    @ApiModelProperty("附件")
    private List<SimpleFileVo> enclosure;

    @ApiModelProperty("报修电话")
    private String phone;

    @ApiModelProperty("报修人")
    private String user;

    @ApiModelProperty("报修科室")
    private Long deptId;

    /** 报修地址 */
    @ApiModelProperty("报修地址")
    private String place;

    public ObsOrderInfo buildOrder(){
        ObsOrderInfo orderInfo = new ObsOrderInfo();
        orderInfo.setDeptId(getDeptId());
        orderInfo.setPlace(getPlace());
        orderInfo.setPhone(getPhone());
        orderInfo.setUser(getUser());
        if(!ObjectUtils.isEmpty(SecurityUtils.getAuthentication())){
            orderInfo.setReception(SecurityUtils.getUserId());
        }
        orderInfo.setOrderState(1);
        orderInfo.setCreateOrderFault(1);
        orderInfo.setCreateTime(DateUtils.getNowDate());
        orderInfo.setDealType(OrderDealTypeConstant.CREATE_ORDER);
        return orderInfo;
    }

    public ObsOrderFault buildOrderFault(ObsOrderInfo orderInfo) {
        ObsOrderFault orderFault = new ObsOrderFault();
        orderFault.setOrderNum(orderInfo.getOrderNum());
        orderFault.setSource(getSource());
        orderFault.setSendType(getSendType());
        orderFault.setFaultType(getFaultType());
        orderFault.setFaultRemark(getFaultRemark());
        orderFault.setDegree(getDegree());
        orderFault.setRangeType(getRangeType());
        orderFault.setResponseType(getResponseType());
        orderFault.setAppointment(getAppointment());
        orderFault.setReqTime(getReqTime());
        orderFault.setLearningId(getLearningId());
        orderFault.setEnclosureId(FileUtils.fileToId(getEnclosure()));
        orderFault.setRecordState(1);
        orderFault.setCurrState(OrderCurrStateConstant.WAIT_DELIVERY);
        orderFault.setCompleteTime(getCompleteTime());
        if(!ObjectUtils.isEmpty(SecurityUtils.getAuthentication())) {
            orderFault.setCreateUser(SecurityUtils.getUserId());
        }
        orderFault.setCreateTime(getCreateTime());
        orderFault.setActualRepairPlace(getActualRepairPlace());
        orderFault.setEquipment(getEquipment());
        orderFault.setEquipmentAddress(getEquipmentAddress());
        orderFault.setEquipmentAddressId(getEquipmentAddressId());
        if(!ObjectUtils.isEmpty(getEquipmentAddressId())){
            orderFault.setEquipmentAddress(DeptUtils.deptIdToPathName(getEquipmentAddressId()));
        }
        orderFault.setAssetsId(getAssetsId());
        return orderFault;
    }

    /**
     * 需要实现继承
     * @return
     */
    public List<ObsOrderSend> buildSend() {
        return null;
    }

    /**
     * 是否需要封装接单人,需要继承实现
     * @return
     */
    public boolean canBuildSend(){
        return false;
    }
}
